#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 20;
int n, a[N], b[N], ans = 1000000000;

void dfs(int i, int x, int y) {
	if (i > n) {
		if (x == 1 && y == 0) return;
		ans = min(ans, abs(x - y));
		return;
	}
	dfs(i + 1, x * a[i], y + b[i]);
	dfs(i + 1, x, y);
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i] >> b[i];
	}
	dfs(1, 1, 0);
	cout << ans << endl;
	return 0;
}